#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using PLACEHR.Web.Utilities;

#endregion

namespace PLACEHR.Web.Categories.Business.Forms
{
    public partial class LeaveApplication : Form
    {
        private Employee employee = Global.CurrentUser;
        private EmployeeVacation currItem;
        private VacationBase vacationItem;
        public LeaveApplication(EmployeeVacation employeeVacation)
        {
            InitializeComponent();
            currItem = employeeVacation;
            
            InitialData();
        }
        private void InitialData()
        {
            lblName.Text = LanLayer.GetTrans(122);
            lblType.Text = LanLayer.GetTrans(123);
            lblStartDate.Text = LanLayer.GetTrans(129);
            lblEndDate.Text = LanLayer.GetTrans(130);
            lblLength.Text = LanLayer.GetTrans(127);
            lblReason.Text = LanLayer.GetTrans(134);
            lblHour.Text = LanLayer.GetTrans(142);
            btnApplication.Text = LanLayer.GetTrans(135);
            btnCancel.Text = LanLayer.GetTrans(136);
            btnPrint.Text = LanLayer.GetTrans(24);
            lblWarning.Text = LanLayer.GetTrans(212);


            txtLeaveName.Text = employee.ChineseName+"("+employee.EnglishName+")";
            cbbLeaveCategory.DataSource = Utility.GetVacationTypeList();
            cbbLeaveCategory.DisplayMember = "value";
            cbbLeaveCategory.ValueMember = "key";
        }
        private void btnApplication_Click(object sender, EventArgs e)
        {
              
            if (Verify())
            {
                currItem = new EmployeeVacation();
                Global.Context.AddToEmployeeVacations(currItem);
                currItem.EmployeeID = employee.EmployeeID;
                
                currItem.VacationTypeCode = Convert.ToString(cbbLeaveCategory.SelectedValue);
                currItem.FromTime = dtpFromDate.Value;
                currItem.EndTime = dtpEndDate.Value;
                currItem.Length = Convert.ToDecimal(txtTotalHours.Text.Trim());
                currItem.UpdateBy = employee.EmployeeID;
                currItem.Note = txtLeaveNote.Text.Trim();
                currItem.InsertTime = DateTime.Now;
                currItem.UpdateTime = DateTime.Now;
                currItem.Status = "Pending";


                currItem.IsDeleted = false;

                Global.Context.SaveChanges();
                //Todo: Send Email
                string office = employee.OfficeDepartment_lnk.Select(o => o.OfficeCode).FirstOrDefault();
                List<string> lstEmails = (from c in Global.Context.Employees 
                                             where c.OfficeDepartment_lnk.Select(p=>p.OfficeCode).FirstOrDefault() == office && c.RoleID == 3
                                             select c.Email).ToList();
                SendEmail.SendEmail sendEmail = new SendEmail.SendEmail();
                sendEmail.Send(string.Join(",",lstEmails), "mail.computersolutions.cn", "hrsystem@sh.placedesigngroup.com", "123456", LanLayer.GetTrans(214), string.Format(LanLayer.GetTrans(215), employee.EnglishName + "(" + employee.ChineseName + ")", dtpFromDate.Value.ToString("yyyy-MM-dd"), dtpEndDate.Value.ToString("yyyy-MM-dd"), txtTotalHours.Text.Trim()));

                Close();
            }
        }

        private bool Verify()
        {
            if (cbbLeaveCategory.SelectedItem == null || string.IsNullOrWhiteSpace(cbbLeaveCategory.Text.ToString()))
            {
                MessageBox.Show(LanLayer.GetTrans(138));
                return false;
            }
            
            string vacationTypeCode = cbbLeaveCategory.SelectedValue.ToString();
            vacationItem = Global.Context.VacationBases.FirstOrDefault(o => o.IsCurrent && o.EmployeeID == employee.EmployeeID && o.IsDeleted == false && o.VacationTypeCode == vacationTypeCode);

            if (!Utility.IsDecimal(txtTotalHours.Text))
            {
                MessageBox.Show(LanLayer.GetTrans(137));
                return false;
            }

            if (Convert.ToString(cbbLeaveCategory.Text) == "ANN" || Convert.ToString(cbbLeaveCategory.Text) == "SKL" || Convert.ToString(cbbLeaveCategory.Text) == "CSL" || Convert.ToString(cbbLeaveCategory.Text) == "LSL")
            {
                if (vacationItem == null || Utility.ToDecimal(txtTotalHours.Text) > vacationItem.FinalHours)
                {
                    MessageBox.Show(LanLayer.GetTrans(139));
                    return false;
                }
            }

            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void lblLeaveName_Click(object sender, EventArgs e)
        {

        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (currItem != null)
            {
                PrintVacationForm form = new PrintVacationForm();
                form.currItem = currItem;
                form.ShowDialog();
            }
        }

        private void LeaveApplication_Load(object sender, EventArgs e)
        {
            if (currItem == null)
            {
                btnPrint.Visible = false;
            }
        }
    }
}